package com.itboye.ihomebank.activity.guanjia;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.BiaoQianBean;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.custom.FlowLayout;
import com.itboye.ihomebank.custom.RoundImageView;
import com.itboye.ihomebank.presenter.RepairPersenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.XImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 评价师傅
 * 
 */
public class ActivityPingJiaWorker extends BaseOtherActivity implements
		Observer {
	View v_statusbar;
	private ImageView close_icon;
	private TextView add_shap_title_tv;
	private FlowLayout fabu_flowLayout01;// 标签
	private RoundImageView pingjia_im;// 头像
	private TextView pingjia_name;// 姓名
	private TextView fabu_house_ok;// 完成
	private RatingBar pingjia_bar;// 评分控件
	private TextView pingjia_cd;// 显示满意程度

	private int xingji=0;// 星级
	private RepairPersenter repairPersenter;
	private List<BiaoQianBean> listBean;
	
	private List<String> text=new ArrayList<String>();

	private RepairPersenter repairPersenter02;

	// 评价标签
	private StringBuffer biao = new StringBuffer();
	private StringBuffer biao02 = new StringBuffer();

	StringBuffer stringB;

	Intent intent;
	String head;
	String name;
	String phone;
	String id;
	String strend;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.fabu_house_ok:

			
			stringB=new StringBuffer();
			for (int i = 0; i < listBean.size(); i++) {
				if(listBean.get(i).isTga()){
					stringB.append(text.get(i)+",");
				}
			}
		//	ByAlert.alert(stringB.toString());
			repairPersenter02 = new RepairPersenter(this);
			repairPersenter02.xiuLiEndAndPingJia(id, xingji==0?3+"":xingji+"", 
					stringB.toString().length() > 0 ? stringB.substring(
					0, stringB.length() - 1) : "");
			
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_pingjia_worker;
	}

	@Override
	protected void onInitController() {

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("评价师傅");

		intent = this.getIntent();
		if (intent != null) {
			head = intent.getStringExtra("head");
			name = intent.getStringExtra("name");
			phone = intent.getStringExtra("phone");
			id = intent.getStringExtra("id");
		}
		XImageLoader.load(head, pingjia_im);
		pingjia_name.setText(name);

		repairPersenter = new RepairPersenter(this);
		repairPersenter.biaoQian();

		listBean = new ArrayList<BiaoQianBean>();
		

		pingjia_bar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
					@Override
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						xingji = (int) rating;

						if (xingji <= 2) {
							pingjia_cd.setText("不满意");
						}
						if (xingji > 2 && xingji <= 4) {
							pingjia_cd.setText("满意");
						}
						if (xingji == 5) {
							pingjia_cd.setText("非常满意");
						}
					}
				});
	}

	/**
	 * 评价标签
	 */
	private void biaoqian() {
		stringB = new StringBuffer();
		if (listBean.size() > 0) {
			for (int i = 0; i < listBean.size(); i++) {
				biao.append(listBean.get(i).getName() + ",");
				biao02.append(listBean.get(i).getId() + ",");
			}
			String[] str = biao.toString().split(",");
			String[] str02 = biao02.toString().split(",");
			listBean=new ArrayList<BiaoQianBean>();
			// 标签
			for (int i = 0; i < str.length; i++) {
				BiaoQianBean b=new BiaoQianBean();
				b.setName(str[i]);
				b.setId(str02[i]);
				listBean.add(b);
				
				View view = LayoutInflater.from(this).inflate(
						R.layout.item_jiaju, null);
				final TextView btn_name = (TextView) view
						.findViewById(R.id.btn_name);
				
				btn_name.setText(str[i]);
				btn_name.setTag(false);
				text.add(str02[i]);
				btn_name.setOnClickListener(new OnClickListener() {

					@SuppressLint("ResourceAsColor")
					public void onClick(View v) {
						TextView t=(TextView) v;
						if((boolean) v.getTag()){
							for (int j = 0; j < listBean.size(); j++) {
								
								if(listBean.get(j).getName().equals(t.getText())){
									listBean.get(j).setTga(false);
								}
							}
							btn_name.setBackgroundResource(R.drawable.shape_gray);
							
						}else{
							for (int j = 0; j < listBean.size(); j++) {
								
								if(listBean.get(j).getName().equals(t.getText())){
									listBean.get(j).setTga(true);
								}
							}
							btn_name.setBackgroundResource(R.drawable.shape_blue02);
						}
						btn_name.setTag(!(boolean)v.getTag());
					}
				});
				MarginLayoutParams layoutParams = new MarginLayoutParams(
						MarginLayoutParams.WRAP_CONTENT,
						MarginLayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(10, 10, 10, 10);
				fabu_flowLayout01.addView(view, layoutParams);
			}
		}
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		// ByAlert.alert(entity.getData());
		if (entity != null) {
			if (!entity.getCode().equals(0)) {

				if (entity.getEventType() == RepairPersenter.biaoQian_success) {
					List<BiaoQianBean> bean = (List<BiaoQianBean>) entity
							.getData();
					if (bean != null) {
						listBean.addAll(bean);
					}
					biaoqian();

				}
				if (entity.getEventType() == RepairPersenter.biaoQian_fail) {
					ByAlert.alert(entity.getData());
				}
			}
			
			if (entity.getEventType() == RepairPersenter.theEndAndPingJia_success) {
				ByAlert.alert(entity.getData());
				
				finish();
			}
			if (entity.getEventType() == RepairPersenter.theEndAndPingJia_fail) {
				ByAlert.alert(entity.getData());
			}

		}

	}

}
